package com.shxc.smarttrade.login.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.NoActionBarBaseActivity;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

/**
 * Kvb登录
 */
@ContentView(R.layout.activity_login_kvb)
public class KvbLoginActivity extends NoActionBarBaseActivity {

    @ViewInject(R.id.kvb_account_textview)
    TextView kvb_account_textview;
    @ViewInject(R.id.kvb_pass_edittext)
    EditText kvb_pass_edittext;
    @ViewInject(R.id.pass_line)
    View pass_line;
    @ViewInject(R.id.visible_imageview)
    ImageView visible_imageview;

    private InputMethodManager imm;
    private boolean canSee = false;
    private int BLUE = Color.parseColor("#141F2B");
    private int GRAY = Color.parseColor("#B3B3B3");



    @Event({R.id.close_imageview,R.id.login_textview,R.id.visible_imageview})
    private void onClickk(View view) {
        switch (view.getId()) {
            case R.id.close_imageview:
                finish();
                break;
            case R.id.login_textview:
                //登录kvb

                break;
            case R.id.visible_imageview:
                canSee = !canSee;
                if (canSee) {
                    visible_imageview.setImageResource(R.mipmap.xianshi_icon);
                    kvb_pass_edittext.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    visible_imageview.setImageResource(R.mipmap.yingcang_icon);
                    kvb_pass_edittext.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imm = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        setListener();
    }

    private void setListener() {

        //判断是否获取焦点 - pass
        kvb_pass_edittext.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    pass_line.setBackgroundColor(BLUE);
                } else {
                    // 此处为失去焦点时的处理内容
                    pass_line.setBackgroundColor(GRAY);
                }
            }
        });
    }

    // 点击EditText以外的任何区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }
}
